<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.min.css" rel="stylesheet">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <link href="/css/style.min.css?v=4.0.0" rel="stylesheet">
    <link href="/common/css/searchStyle.css" rel="stylesheet">
    <link href="/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <base target="_blank">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="seach-context">
                    <label>表名 :</label><input id="tableName" placeholder="请输入查询的表名">
                </div>
                <button id="resetBtn" type="button" class="btn btn-w-m btn-success">重置</button>
                <button id="searchBtn" type="button" class="btn btn-w-m btn-success">查询</button>
                <button id="noCheckBtn" type="button" class="btn btn-w-m btn-success">清除选中</button>
                <button id="genBtn" type="button" class="btn btn-w-m btn-success">生成</button>
                <div class="ibox-content">
                    <table id="sysRoleTable">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.5"></script>
<script src="/js/content.min.js?v=1.0.0"></script>
<script src="/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
<script src="/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/js/plugins/validate/messages_zh.min.js"></script>
<script src="/js/plugins/layer-pc/layer.js"></script>
<script src="/common/form-validator.js"></script>
<script src="/common/bootstrap-table.js"></script>
<script src="/common/hmtool.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script type="text/javascript">
    var id = GetQueryString('id'),
    assignArr = [];
    $(function () {
        $('#resetBtn').click(function () {
            refreshGrid('sysRoleTable');
        });

        $('#searchBtn').click(function () {
            refreshGrid('sysRoleTable');
        });

        $('#noCheckBtn').click(function () {
            assignArr = [];
            refreshGrid('sysRoleTable');
        });
        $('#genBtn').click(function(){
            if(assignArr.length > 0){
                layer.confirm('是否生成表['+assignArr.join(',')+"]代码",function(pIndex){
                    layer.prompt({title: '请输入生成出来的顶级包结构'}, function(text, index){
                        var req = {};
                        req['reqJSON'] = {
                            type: 'get',
                            url: SSO_URL + 'gb/database/generator?id=' + id + '&packages='+text+'&tables='+assignArr.join(','),
                            data: {}
                        };
                        req['successFn'] = function (respData) {
                            if(respData['code'] == SUCCESS_CODE){
                                layer.prompt({title: '请输入下载的文件名！！'}, function(path, subIndex){
                                    window.open(SSO_URL + 'gb/database/download?id=' + respData['data'] + '&fileName='+path);
                                    layer.close(subIndex);
                                    layer.close(index);
                                    layer.close(pIndex);
                                });
                            }else{
                                layer.msg(respData['message']);
                            }
                        };
                        ajaxRequest(req);
                    });
                });
            }else{
                layer.msg('请选择要生成的表');
            }

        });
        var layerIndex = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        var req = {};
        req['reqJSON'] = {
            type: 'get',
            url: SSO_URL + 'gb/database/check?id=' + id,
            data: {}
        };
        req['successFn'] = function (respData) {
            layer.close(layerIndex);
            layer.msg(respData.data, {icon: 6}, function () {
            });
            if(respData.code == SUCCESS_CODE){
                var table = {};
                table['id'] = 'sysRoleTable';
                table['url'] = SSO_URL + 'gb/database/pageTableById';
                table['pagination'] = true;
                table['queryParams'] = function (params) {
                    var queryParams = queryBaseParams(params);
                    queryParams['id'] = id;
                    queryParams['tableName'] = $('#tableName').val();
                    return queryParams;
                };
                table['columns'] = [{
                    checkbox: true,
                    field: 'checkAssign',
                    formatter: function (value, row, index) {
                        if (assignArr.indexOf(row['tableName']) > -1) {
                            return true;
                        }
                        return false;
                    }
                },{
                    field: 'tableName',
                    title: '表名'
                }, {
                    field: 'engine',
                    title: '引擎'
                }, {
                    field :'tableCollation',
                    title:'表字符集'
                },{
                    field:'tableComment',
                    title:'表备注'
                },{
                    field : 'createTime',
                    title : '创建时间'
                },{
                    field: "opt",
                    title: "操作",
                    width: 200,
                    formatter: function (value, row, index) {
                        return '<a class="code" title="字段查看">字段查看</a>&nbsp;&nbsp;&nbsp;';
                    },
                    events: {
                        'click .code': function (e, value, row, index) {
                            let layIndex = layer.open({
                                type: 2,
                                title: '表名:'+row['tableComment']+'字段',
                                shade: 0.8,
                                area: ['380px', '80%'],
                                content: ['/gb/database/code.html?tableName='+row['tableName'], 'auto'], //iframe的url，no代表不显示滚动条
                            });
                            layer.full(layIndex);
                        }
                    }
                }];
                initTable(table);

                $('#sysRoleTable').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table', function (e, rows) {
                    debugger;
                    if (e.type == 'uncheck') {
                        var index = assignArr.indexOf(rows['tableName']);
                        if (index > -1) {
                            assignArr.splice(index, 1);
                        }
                    } else if (e.type == 'check') {
                        var index = assignArr.indexOf(rows['tableName']);
                        if (index == -1) {
                            assignArr.push(rows['tableName']);
                        }

                    } else if (e.type == 'uncheck-all') {
                        $.each(rows, function (index, row) {
                            var rowIndex = assignArr.indexOf(row['tableName']);
                            if (rowIndex > -1) {
                                assignArr.splice(rowIndex, 1);
                            }
                        });
                    } else if (e.type == 'check-all') {
                        $.each(rows, function (index, row) {
                            var rowIndex = assignArr.indexOf(row['tableName']);
                            if (rowIndex == -1) {
                                assignArr.push(row['tableName']);
                            }
                        });
                    }
                });
            }
        };
        ajaxRequest(req);
    });
</script>
</body>
</html>